package com.shop.zzh.service.impl;

import com.shop.zzh.entity.User;
import com.shop.zzh.mbg.mapper.UserMapper;
import com.shop.zzh.mbg.mapper.UserPermissionMapper;
import com.shop.zzh.mbg.mapper.UserPermissionRoleMapper;
import com.shop.zzh.mbg.model.UserPermission;
import com.shop.zzh.mbg.model.UserPermissionRole;
import com.shop.zzh.service.UserService;
import com.shop.zzh.utils.JwtTokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @program: shop
 * @description: 实现类
 * @author: zhuzh
 * @create: 2019-11-19 20:57
 **/
@Service
@Slf4j
@Transactional
public class UserServiceImpl implements UserService {
	private static final Logger LOGGER =LoggerFactory.getLogger(UserServiceImpl.class);
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private UserDetailsService userDetailsService;
	@Autowired
	private UserPermissionMapper userPermissionMapper;
	@Autowired
	private UserPermissionRoleMapper userPermissionRoleMapper;
	@Autowired
	private JwtTokenUtil jwtTokenUtil;
	@Autowired
	private PasswordEncoder passwordEncoder;
	@Value("${jwt.tokenHead}")
	private String tokenHead;


	@Override
	public String login(User user) {
		String token = null;
		try {
			UserDetails userDetails = userDetailsService.loadUserByUsername(user.getUserName());
			if (!passwordEncoder.matches(user.getPassword(), userDetails.getPassword())) {
				throw new BadCredentialsException("密码不正确");
			}
			UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
			SecurityContextHolder.getContext().setAuthentication(authentication);
			token = jwtTokenUtil.generateToken(userDetails);
			LOGGER.info("登录成功:{}", token);
		} catch (AuthenticationException e) {
			LOGGER.warn("登录异常:{}", e.getMessage());
		}
		return token;
	}

	@Override
	public void register(User user) {
		userMapper.register(user);
	}

	@Override
	public User selectOnByUserName(String userName) {
		return userMapper.selectOnByUserName(userName);
	}

	@Override
	public List<UserPermission> getPermissionList(String id) {
		List<UserPermission> list = new ArrayList();
		UserPermissionRole permissionRole = new UserPermissionRole();
		permissionRole.setUserId(id);
		List<UserPermissionRole> list1 = userPermissionRoleMapper.selectByUserPermissionRole(permissionRole);
		UserPermission userPermission = null;
		for (UserPermissionRole userPermissionRole:list1){
			userPermission = userPermissionMapper.selectByPrimaryKey(userPermissionRole.getPermissionId());
			list.add(userPermission);
		}
		return list;
	}
}
